package atomlist;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;

/**
 * @功能
 * @创建时间 2019/12/21
 * https://blog.csdn.net/sunxianghuang/article/details/52221913
 */
public class T01_ConcurrentMap {
    public static void main(String[] args) {
  //      Map<String,String> map = new ConcurrentHashMap<>();  //并发性比较高
 //       Map<String,String> map = new ConcurrentSkipListMap<>(); //高并发并且排序
        Map<String,String> map = new Hashtable<>(); //并发性不高
 //       Map<String,String> map = new HashMap<>();
 //       Map<String,String> map1= Collections.synchronizedMap(map); //加锁的map
        Random r = new Random();
        Thread[] ths = new Thread[100];
        CountDownLatch latch = new CountDownLatch(ths.length);
        long start = System.currentTimeMillis();
        for(int i=0;i<ths.length;i++){
            ths[i] = new Thread(()->{
               for(int j=0;j<1000;j++)
                   map.put("a"+r.nextInt(100000),"a"+r.nextInt(100000)) ;
               latch.countDown();
            });

        }
        Arrays.asList(ths).forEach(t->t.start());
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        System.out.println(end );
        System.out.println(start);
    }
}
